﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDAProductCategory
    {

        public static List<ProductCategory> Select(ProductCategory productCategory)
        {
            MySqlConnection  con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();


            string _cmdText =
                 "SELECT pc.* " +
                 "FROM productcategory pc " +
                 "WHERE ((pc.ID = " + productCategory.ID.ToString() + ") OR (" + productCategory.ID.ToString() + " = " + ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString() + "))" +
                 "ORDER BY pc.ID";

            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<ProductCategory> _productCategoryList = new List<ProductCategory>();

            foreach (DataRow row in tbl.Rows)
            {
                ProductCategory _productCategory = new ProductCategory();

                _productCategory.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                _productCategory.Name = (row["Name"].ToString() == "" ? null : Convert.ToString(row["Name"]));
                _productCategory.BgImage = (row["bgImage"].ToString() == "" ? null : Convert.ToString(row["bgImage"]));
                _productCategory.Image = (row["Image"].ToString() == "" ? null : Convert.ToString(row["Image"]));

                _productCategoryList.Add(_productCategory);
            }
           
            con.Close();
            return _productCategoryList;
        }
    }
}